//
//  SearchScenicNavBar.swift
//  LeTu
//
//  Created by g1 on 2024/1/14.
//

import UIKit

class SearchScenicNavBar: UIView {

    private var isClearContent = true
    var rightItemHolder: (() -> Void)?
    var searchHolder: ((String) -> Void)?
    
    init(placeholder: String, isClearContent: Bool = true) {
        super.init(frame: .zero)

        self.isClearContent = isClearContent
        configUI()
        searchTextField.placeholder = placeholder
        
    }
    init(placeholder: String, rightName: String = "", isClearContent: Bool = true) {
        super.init(frame: .zero)

        self.isClearContent = isClearContent
        configUI()
        searchTextField.placeholder = placeholder
        
        rightIconBtn.isHidden = rightName.count == 0
        rightIconBtn.setTitle(rightName, for: .normal)
        rightIconBtn.snp.updateConstraints { make in
            make.width.equalTo(rightName.count == 0 ? 0:rightName.widthWithLimit(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 40), font: kSetPingFangRegular(16))+6)
        }
        searchBackView.snp.updateConstraints { make in
            make.right.equalTo(rightIconBtn.snp.left).offset(rightName.count == 0 ? 0:-10)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    func setKeyword(_ keyword: String) {
        searchTextField.text = keyword
    }
    func becomeSearch() {
        searchTextField.becomeFirstResponder()
    }
    
    // MARK: - UIButtonAction
    @objc func searchAction() {
        guard let holder = searchHolder, let keyword = searchTextField.text, keyword.count > 0 else { return }
        if isClearContent {
            searchTextField.text = nil
        }
        holder(keyword)
    }
    @objc func rightIconBtnAction() {
        guard let holder = rightItemHolder else { return }
        holder()
    }
    @objc func searchIconBtnAction() {
        searchTextField.becomeFirstResponder()
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(rightIconBtn)
        backView.addSubview(searchBackView)
        searchBackView.addSubview(searchTextField)

        backView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalTo(2)
            make.right.equalTo(0)
            make.height.equalTo(Size.navigationBarWithoutStatusHeight)
        }
        rightIconBtn.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.width.equalTo(68)
        }
        searchBackView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(rightIconBtn.snp.left)
            make.centerY.equalToSuperview()
            make.height.equalTo(38)
        }
        searchTextField.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.bottom.equalToSuperview()
        }
        
        rightIconBtn.addTarget(self, action: #selector(rightIconBtnAction), for: .touchUpInside)
        searchTextField.delegate = self
        
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var searchBackView: UIView = {
        let searchBackView = UIView()
        searchBackView.layer.cornerRadius = 18
        searchBackView.layer.masksToBounds = true
        searchBackView.backgroundColor = .app_viewBackgroundColor
        return searchBackView
    }()
    private lazy var searchTextField: UITextField = {
        let searchTextField = UITextField()
        searchTextField.font = kSetPingFangRegular(14)
        searchTextField.returnKeyType = .done
        searchTextField.clearButtonMode = .always
        let imgBackView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 14))
        let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 14, height: 14))
        imgView.image = UIImage(named: "search_keyword_icon")
        imgView.contentMode = .scaleAspectFit
        imgBackView.addSubview(imgView)
        searchTextField.leftView = imgBackView
        searchTextField.leftViewMode = .always
        return searchTextField
    }()
    private lazy var rightIconBtn: AppCustomButton = {
        let rightIconBtn = AppCustomButton()
        rightIconBtn.titleLabel?.font = kSetPingFangRegular(16)
        rightIconBtn.setTitleColor(.app_black, for: .normal)
        rightIconBtn.setTitle("取消".localized, for: .normal)
        return rightIconBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension SearchScenicNavBar: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        if let text = textField.text, text.count > 0 {
            searchAction()
            textField.resignFirstResponder()
        }
        
        return true
    }
    
}
